/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef SYSTEM_EVENT_WIN32_H_
#define SYSTEM_EVENT_WIN32_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include "Ucop/Exception.h"


namespace Ucop {
namespace System {

    class EventImpl
    {
    protected:
        EventImpl(bool _AutoReset, bool _InitState, const char *_Name)
        {
            _Myhandle = CreateEvent(NULL, !_AutoReset, _InitState, _Name);
            if (!_Myhandle) {
                throw SystemException("create event failed");
            }
        }

        ~EventImpl(void)
        {
            CloseHandle(_Myhandle);
        }


        bool SetImpl(void)
        {
            return SetEvent(_Myhandle) != FALSE;
        }

        bool ResetImpl(void)
        {
            return ResetEvent(_Myhandle) != FALSE;
        }

        bool WaitImpl(void)
        {
            return WaitForSingleObject(_Myhandle, INFINITE) != WAIT_FAILED;
        }

        bool WaitImpl(long _Miliseconds)
        {
            return WaitForSingleObject(_Myhandle, (DWORD)_Miliseconds) != WAIT_FAILED;
        }

    private:
        HANDLE _Myhandle;
    };

}}

#endif // SYSTEM_EVENT_WIN32_H_
